<template>
  <div id="detail">
    <!-- 导航栏 -->
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav" />
    <!-- 滚动插件 -->
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
    >
      <!-- 轮播图 -->
      <detail-swiper :top-images="topImages" />
      <!-- 商品信息 -->
      <detail-base-info :goods="goods" />
      <!-- 店铺信息 -->
      <detail-shop-info :shop="shop" />
      <!-- 商品详情信息 -->
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad" />
      <!-- 商品参数信息 -->
      <detail-param-info ref="params" :param-info="paramInfo" />
      <!-- 商品评论信息 -->
      <detail-comment-info ref="comment" :comment-info="commentInfo" />
      <!-- 推荐商品 -->
      <goods-list ref="recommend" :goods="recommends" />
    </scroll>
    <!-- 底部 -->
    <detail-bottom-bar @addCart="addToCart" />
    <!-- 回到顶部按钮 -->
    <back-top @click.native="backClick" v-show="isShowBackTop" />

    <!-- 提示框 -->
    <!-- <toast :message="message" :show="show" /> -->
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import DetailBottomBar from "./childComps/DetailBottomBar";

import Scroll from "components/common/scroll/Scroll";
import GoodsList from "components/content/goods/GoodsList";

import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend
} from "network/detail";
import { debounce } from "common/utils";
import { itemListenerMixin, backTopMixin } from "common/mixin";

import { mapActions } from "vuex";

// import Toast from "../../components/common/toast/Toast"

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar
    // Toast
  },
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0
      // message: '',
      // show: false
    };
  },
  created() {
    // 保存传入的 iid
    this.iid = this.$route.params.iid;

    // 根据 iid 请求数据
    getDetail(this.iid).then(res => {
      // console.log(res);
      const data = res.result;
      // 1. 获取顶部图片轮播数据
      this.topImages = data.itemInfo.topImages;

      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 3.获取店铺信息
      this.shop = new Shop(data.shopInfo);

      // 4.保存商品详情数据
      this.detailInfo = data.detailInfo;

      // 5.获取商品参数数据
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      // 6.获取评论信息数据
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    });

    // 请求商品推荐数据
    getRecommend().then(res => {
      this.recommends = res.data.list;
    });

    // 给getThemeTopY赋值（对给this.themeTopYs赋值的操作进行防抖）
    this.getThemeTopY = debounce(() => {
      // 获取参数、评论、推荐信息到顶部的距离
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop );
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop );
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop );
      this.themeTopYs.push(Number.MAX_VALUE);
    }, 600);
  },
  methods: {
    // 映射 vux 里面的 actions
    ...mapActions(["addCart"]),

    // 监听图片加载
    imageLoad() {
      // 自定义节流
      // this.$refs.scroll.refresh()
      // 防抖方法
      this.newrefresh();

      this.getThemeTopY();
    },

    // 点击顶部标题  滑动到相应位置
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 300);
    },

    // 监听滚动事件
    contentScroll(position) {
      // 获取 y 值
      const positoinY = -position.y;

      // 2.positoinY和主题中的值进行对比
      let lengthTop = this.themeTopYs.length;
      for (let i = 0; i < lengthTop - 1; i++) {
        if (
          this.currentIndex !== i &&
          positoinY >= this.themeTopYs[i] &&
          positoinY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }

      // 3。是否显示回到顶部
      this.isShowBackTop = position.y < -900;
    },

    // 监听点击购物车按钮
    addToCart() {
      // 1.获取购物车需要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      // 2.将商品添加到购物车
      // 方法1
      this.addCart(product).then(res => {
        // this.show = true;
        // this.message = res;

        // setTimeout(() => {
        //   this.show = false
        // }, 1500)

        this.$toast.show(res);
      });

      // this.$store.commit("addCart", product);
      // 方法2
      // this.$store.dispatch("addCart", product).then(res => {
      //   console.log(res);
      // })
    }
  },
  // 生命周期函数
  mounted() {},
  destroyed() {
    this.$bus.$off("itemImageLoad", this.itemImgListener);
  }
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 7;
  background-color: #fff;
  height: 100vh;
}
.detail-nav {
  position: relative;
  z-index: 6;
  background-color: #fff;
}

.content {
  height: calc(100% - 44px - 58px);
}
</style>
